package org.jeecg.modules.business.util;

import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.business.entity.BusDict;
import org.jeecg.modules.business.model.BusDictTreeModel;
import org.jeecg.modules.business.model.DictIdModel;

import java.util.ArrayList;
import java.util.List;

public class FindsBusDictsChildrenUtil {

    /**
     * queryTreeList的子方法 ====1=====
     * 该方法是BusDict类型的list集合转换成BusDictTreeModel类型的集合
     */
    public static List<BusDictTreeModel> wrapTreeDataToTreeList(List<BusDict> recordList) {
        // 在该方法每请求一次,都要对全局list集合进行一次清理
        //idList.clear();
        List<DictIdModel> idList = new ArrayList<DictIdModel>();
        List<BusDictTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            BusDict depart = recordList.get(i);
            records.add(new BusDictTreeModel(depart));
        }
        List<BusDictTreeModel> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

    /**
     * 获取 DictIdModel
     * @param recordList
     * @return
     */
    public static List<DictIdModel> wrapTreeDataToDepartIdTreeList(List<BusDict> recordList) {
        // 在该方法每请求一次,都要对全局list集合进行一次清理
        //idList.clear();
        List<DictIdModel> idList = new ArrayList<DictIdModel>();
        List<BusDictTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            BusDict busDict = recordList.get(i);
            records.add(new BusDictTreeModel(busDict));
        }
        findChildren(records, idList);
        return idList;
    }

    /**
     * queryTreeList的子方法 ====2=====
     * 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    private static List<BusDictTreeModel> findChildren(List<BusDictTreeModel> recordList,
                                                         List<DictIdModel> dictIdList) {

        List<BusDictTreeModel> treeList = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            BusDictTreeModel branch = recordList.get(i);
            if (oConvertUtils.isEmpty(branch.getParentId()) || branch.getParentId().equals("0")) {
                treeList.add(branch);
                DictIdModel departIdModel = new DictIdModel().convert(branch);
                dictIdList.add(departIdModel);
            }
        }
        getGrandChildren(treeList,recordList,dictIdList);

        //idList = departIdList;
        return treeList;
    }

    /**
     * queryTreeList的子方法====3====
     *该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
     */
    private static void getGrandChildren(List<BusDictTreeModel> treeList,List<BusDictTreeModel> recordList,List<DictIdModel> idList) {

        for (int i = 0; i < treeList.size(); i++) {
            BusDictTreeModel model = treeList.get(i);
            DictIdModel idModel = idList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                BusDictTreeModel m = recordList.get(i1);
                if (m.getParentId()!=null && m.getParentId().equals(model.getId())) {
                    model.getChildren().add(m);
                    DictIdModel dim = new DictIdModel().convert(m);
                    idModel.getChildren().add(dim);
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren());
        }

    }


    /**
     * queryTreeList的子方法 ====4====
     * 该方法是将子节点为空的List集合设置为Null值
     */
    private static void setEmptyChildrenAsNull(List<BusDictTreeModel> treeList) {

        for (int i = 0; i < treeList.size(); i++) {
            BusDictTreeModel model = treeList.get(i);
            if (model.getChildren().size() == 0) {
                model.setChildren(null);
                model.setIsLeaf(true);
            }else{
                setEmptyChildrenAsNull(model.getChildren());
                model.setIsLeaf(false);
            }
        }
        // sysDepartTreeList = treeList;
    }
//    /**
//     * queryTreeList的子方法 ====1=====
//     * 该方法是将BusDict类型的list集合转换成BusDictTreeModel类型的集合
//     */
//    public static List<BusDictTreeModel> wrapTreeDataToTreeList(List<BusDict> recordList) {
//        // 在该方法每请求一次,都要对全局list集合进行一次清理
//        //idList.clear();
//        List<DictIdModel> idList = new ArrayList<DictIdModel>();
//        List<BusDictTreeModel> records = new ArrayList<>();
//        for (int i = 0; i < recordList.size(); i++) {
//            BusDict depart = recordList.get(i);
//            records.add(new BusDictTreeModel(depart));
//        }
//        List<BusDictTreeModel> tree = findChildren(records, idList);
//        setEmptyChildrenAsNull(tree);
//        return tree;
//    }
//    /**
//     * queryTreeList的子方法 ====2=====
//     * 该方法是找到并封装顶级父类的节点到TreeList集合
//     */
//    private static List<BusDictTreeModel> findChildren(List<BusDictTreeModel> recordList,
//                                                         List<DictIdModel> departIdList) {
//
//        List<BusDictTreeModel> treeList = new ArrayList<>();
//        for (int i = 0; i < recordList.size(); i++) {
//            BusDictTreeModel branch = recordList.get(i);
//            if (oConvertUtils.isEmpty(branch.getParentId())) {
//                treeList.add(branch);
//                DictIdModel departIdModel = new DictIdModel().convert(branch);
//                departIdList.add(departIdModel);
//            }
//            treeList.add(branch);
//        }
//        getGrandChildren(treeList,recordList);
//
//        //idList = departIdList;
//        return treeList;
//    }
//    /**
//     * queryTreeList的子方法====3====
//     *该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
//     */
//    private static void getGrandChildren(List<BusDictTreeModel> treeList,List<BusDictTreeModel> recordList) {
//
//        for (int i = 0; i < treeList.size(); i++) {
//            BusDictTreeModel model = treeList.get(i);
//            for (int i1 = 0; i1 < recordList.size(); i1++) {
//                BusDictTreeModel m = recordList.get(i1);
//                if (m.getParentId()!=null && m.getParentId().equals(model.getId())) {
//                    model.getChildren().add(m);
//                }
//            }
//            getGrandChildren(treeList.get(i).getChildren(), recordList);
//        }
//
//    }
//    /**
//     * queryTreeList的子方法 ====4====
//     * 该方法是将子节点为空的List集合设置为Null值
//     */
//    private static void setEmptyChildrenAsNull(List<BusDictTreeModel> treeList) {
//
//        for (int i = 0; i < treeList.size(); i++) {
//            BusDictTreeModel model = treeList.get(i);
//            if (model.getChildren().size() == 0) {
//                model.setChildren(null);
//                model.setIsLeaf(true);
//            }else{
//                setEmptyChildrenAsNull(model.getChildren());
//                model.setIsLeaf(false);
//            }
//        }
////         sysDepartTreeList = treeList;
//    }
}
